//
//  DraggableImage.m
//  NCMM
//

#import "DraggableImage.h"

@implementation DraggableImage

//@synthesize currentPoint;
@synthesize imageID;

- (id)initWithImage:(UIImage *)image
{
    if (self = [super initWithImage:image])
        self.userInteractionEnabled = YES;
    return self;
}

// - (void) snapMouth

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
	// When a touch starts, get the current location in the view
	currentPoint = [[touches anyObject] locationInView:self];
}

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
	// Get active location upon move
	CGPoint activePoint = [[touches anyObject] locationInView:self];
    
	// Determine new point based on where the touch is now located
	CGPoint newPoint = CGPointMake(self.center.x + (activePoint.x - currentPoint.x),
                                   self.center.y + (activePoint.y - currentPoint.y));
    
	//--------------------------------------------------------
	// Make sure we stay within the bounds of the parent view
	//--------------------------------------------------------
    float midPointX = CGRectGetMidX(self.bounds);
	// If too far right...
    if (newPoint.x > self.superview.bounds.size.width  - midPointX)
        newPoint.x = self.superview.bounds.size.width - midPointX;
	else if (newPoint.x < midPointX) 	// If too far left...
        newPoint.x = midPointX;
    
	float midPointY = CGRectGetMidY(self.bounds);
    // If too far down...
	if (newPoint.y > self.superview.bounds.size.height  - midPointY)
        newPoint.y = self.superview.bounds.size.height - midPointY;
	else if (newPoint.y < midPointY)	// If too far up...
        newPoint.y = midPointY;
    
	// Set new center location
	self.center = newPoint;
	//    NSLog(@"X: %f Y: %f", newPoint.x, newPoint.y);
}

- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
	//DORSAL REGION: (0, 400, 240, 150)
    if (self.center.x >= 443 && self.center.x <= 761 && self.center.y >= 133 && self.center.y <= 359 && self.imageID == 1)
    {
        self.center = CGPointMake(650, 200);
		self.userInteractionEnabled = NO;
    }
	//EYES REGION: (0, 400, 240, 150)
    if (self.center.x >= 119 && self.center.x <= 205 && self.center.y >= 373 && self.center.y <= 417 && self.imageID == 2)
    {
        self.center = CGPointMake(85, 320);
		self.userInteractionEnabled = NO;
    }
	//CAUDAL REGION: (0, 400, 240, 150)
    if (self.center.x >= 767 && self.center.x <= 963 && self.center.y >= 243 && self.center.y <= 537 && self.imageID == 3)
    {
        self.center = CGPointMake(870, 520);
		self.userInteractionEnabled = NO;
    }
	//MOUTH REGION: (0, 400, 240, 150)
    if (self.center.x >= 0 && self.center.x <= 240 && self.center.y >= 400 && self.center.y <= 550 && self.imageID == 4)
    {
        self.center = CGPointMake(90, 480);
		self.userInteractionEnabled = NO;
    }
	
	//Probably better code here:
	//if ([self inRegionPX:newPoint.x PY:newPoint.y X1:0 X2:240 Y1:400 Y2:550])
	//{
	//	NSLog(@"in mouth");
	//}
}

//- (BOOL) inRegionPX:(int)px PY:(int)py X1:(int)x1 Y1:(int)y1 X2:(int)x2 Y2:(int)y2
//{
//	return (px >= x1 && px <= x2 && py >= y1 && py <= y2);
//}

@end
